home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18379 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  103 lines

  1. Path: ix.netcom.com!news
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: String operator+ and memory leakage.
  5. Date: Fri, 19 Apr 1996 16:02:18 GMT
  6. Organization: Netcom
  7. Message-ID: <3177b7d5.149465039@nntp.ix.netcom.com>
  8. References: <4l5foo$fen@utopia.hacktic.nl>
  9. NNTP-Posting-Host: ix-dc14-19.ix.netcom.com
  10. X-NETCOM-Date: Fri Apr 19 11:05:51 AM CDT 1996
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Mike Tavares <MIKET@cdynamics.com> wrote:
  14.  
  15. > I have an implementation (not mine) of the String class that is leaking 
  16. > memory
  17. > during the + operator code.  The code follows:
  18. > class String
  19. >     {
  20. >     public:
  21. >         // Constructors/destructors
  22. >         String();
  23. >         String( char* new_string);
  24. >         String( String& new_string);
  25. >         ~String()
  26. >             {
  27. >             delete character_array;
  28. >             }
  29. >         String&     operator+( String& a_string );
  30. >         String&     operator=( String& a_string );
  31. >         operator    char* () { return character_array; }
  32. >     private:
  33. >         char* character_array;
  34. >         int string_length;
  35. >     };
  36. > String&
  37. > String::operator+( String& a_string )
  38. >     {
  39. >     int total_size = string_length + strlen( a_string ) + 1;
  40. >     char* temp_array = new char[total_size];
  41. >     char* char_array = a_string;
  42. >     strcpy( temp_array, character_array );
  43. >     strcat( temp_array, char_array );
  44. >     String* new_string = new String( temp_array );
  45. >     delete [] temp_array;
  46. >     return *new_string;
  47. >     }
  48. > String&
  49. > String::operator=( String& a_string )
  50. >     {
  51. >     char* new_char_array = a_string;
  52. >     delete [] character_array;
  53. >     character_array = new char[strlen( new_char_array ) + 1];
  54. >     strcpy( character_array, new_char_array );
  55. >     string_length = strlen( character_array );
  56. >     return *this;
  57. >     }
  58. > My usage is:
  59. > destString = string1 + string2 + string3...;
  60. > The problem is, when new_string is created and returned through a 
  61. > reference, no one ever deletes it.
  62. > If I change new_string to be an automatic variable I get an error from 
  63. > the compiler about trying to pass a reference to a item that is out of 
  64. > scope.
  65. > If I change the method to work in this I mutate one of my addends.  There 
  66. > has to be a way of implementing this without memory leakage! HELP!
  67.  
  68. Don't return a reference.  Something like
  69.  
  70.     String&
  71.     String::operator+( String& a_string )
  72.         {
  73.           int total_size = string_length + strlen( a_string ) + 1;
  74.           char* temp_array = new char[total_size];
  75.       char* char_array = a_string;
  76.  
  77.       strcpy( temp_array, character_array );
  78.       strcat( temp_array, char_array );
  79.  
  80.       String new_string(temp_array);
  81.       delete [] temp_array;
  82.       return new_string;
  83.     }
  84.  
  85.  
  86. Michael M Rubenstein
  87.